import { CreateElement, VNode } from 'vue';
import { Vue, Component, Inject } from 'vue-property-decorator';

import { ListViewContext, isFunction, getWidget } from '../../vendors/handie';

import { ComponentCtor } from '../../types/component';

@Component({
  // @ts-ignore
  abstract: true,
})
export default class SearchRenderer extends Vue {
  @Inject({ from: 'viewContext', default: null })
  private readonly context!: ListViewContext;

  private render(h: CreateElement): VNode | null {
    const search = this.context.getSearch();

    let widgetCtor: ComponentCtor;

    if (isFunction(search)) {
      widgetCtor = search as ComponentCtor;
    } else {
      const { widget = 'FormSearchWidget' } = search as any;

      widgetCtor = isFunction(widget) ? widget : getWidget(widget);
    }

    return h(widgetCtor);
  }
}
